package com.tlw.practice.tlw.exception;

import com.tlw.practice.tlw.common.CommonResult;
import com.tlw.practice.tlw.common.ResultCode;
import lombok.Data;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一捕获异常
 */
@ControllerAdvice
public class GlobalExceptionHandler extends RuntimeException{

    /**
     * 处理全局异常
     * @param e
     * @return
     */
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public CommonResult myExceptionHandler(Exception e){
            e.printStackTrace();
            return CommonResult.error(ResultCode.SERVER_IS_BUSY);
        }

    /**
     * 处理业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public CommonResult error(Exception e){
        e.printStackTrace();
        return CommonResult.error(ResultCode.SERVER_IS_BUSY);
    }

        public WebServerFactoryCustomizer createWebServerFactoryCustomizer(){
            WebServerFactoryCustomizer factoryCustomizer = new WebServerFactoryCustomizer() {
                @Override
                public void customize(WebServerFactory factory) {

                }
            };
            return factoryCustomizer;
        }
}
